import type { ResponseStruct } from '#/global'

/**
 * 树节点类型
 */
export interface TreeNode {
    id: number
    name: string
    type: 'zone' | 'proj_line' | 'area' | 'stand' | 'rod'
    children?: TreeNode[]
    broadcast?: BroadcastDevice[]
}

/**
 * 广播设备
 */
export interface BroadcastDevice {
    id: number
    name: string
    server_id: number
    device_id: number  // 喊话时使用的设备ID
    status: number  // 设备在线状态，1-在线，0-离线
}

/**
 * 长连接参数
 */
export interface ConnectionParams {
    ws_url: string
    user: string
    pwd: string
}

/**
 * 用户认证信息
 */
export interface UserAuth {
    username: string
    password: string
}

/**
 * 广播服务器信息
 */
export interface BroadcastServer {
    id: number
    name: string
    link: string  // 服务器地址，格式: http://ip:port
    username: string
    password: string
    remark?: string
    created_at: string
    updated_at: string
    created_by: number
    updated_by?: number
    auth: string
}

/**
 * 广播服务器列表响应
 */
export interface BroadcastServerListResponse {
    list: BroadcastServer[]
    total: number
}

/**
 * 获取设备树
 * @returns
 */
export function tree(data?: any): Promise<ResponseStruct<TreeNode[]>> {
    return useHttp().get('device/rdDeviceRod/tree', { params: data })
}

/**
 * 获取用户认证信息
 * @returns
 */
export function getUserAuth(): Promise<ResponseStruct<UserAuth>> {
    return useHttp().get('device/rdDeviceRod/user-auth')
}

/**
 * 获取广播服务器列表
 * @returns
 */
export function getBroadcastServerList(): Promise<ResponseStruct<BroadcastServerListResponse>> {
    return useHttp().get('device/rdDeviceBroadcastServer/list')
}

/**
 * 获取长连接参数
 * @returns
 */
export function getConnectionParams(): Promise<ResponseStruct<ConnectionParams>> {
    return useHttp().get('device/rdDeviceRod/connection-params')
}

/**
 * 创建广播记录参数
 */
export interface CreateBroadcastRecordParams {
    type: 1 | 2  // 1:广播 2:喊话
    device: number[]  // 设备device_id数组
    start_time: number  // 开始时间戳（秒）
    end_time: number  // 结束时间戳（秒）
}

/**
 * 创建广播记录
 * @param data 
 * @returns 
 */
export function createBroadcastRecord(data: CreateBroadcastRecordParams): Promise<ResponseStruct<any>> {
    return useHttp().post('alarm/rdAlarmBroadcastRecord', data)
}
